package cn.modoumama.springboot.dubbo;

import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import com.alibaba.dubbo.qos.common.Constants;

public class DubboConfigurationApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        Environment env = applicationContext.getEnvironment();
        String scan = env.getProperty("spring.dubbo.scan");
        if (scan != null) {
            AnnotationBean scanner = BeanUtils.instantiate(AnnotationBean.class);
            scanner.setPackage(scan);
            scanner.setApplicationContext(applicationContext);
            applicationContext.addBeanFactoryPostProcessor(scanner);
            applicationContext.getBeanFactory().addBeanPostProcessor(scanner);
            applicationContext.getBeanFactory().registerSingleton("annotationBean", scanner);
        }
        String qosPort = env.getProperty("spring.dubbo.qos.port");
        if(StringUtils.isNotEmpty(qosPort)){
        	   System.setProperty(Constants.QOS_PORT, qosPort);
        }
        
        
        String acceptForeignIp = env.getProperty("spring.dubbo.accept.foreign.ip");
        if(StringUtils.isNotEmpty(acceptForeignIp)){
        	 System.setProperty(Constants.ACCEPT_FOREIGN_IP, acceptForeignIp);
        }
        
     

    }


}
